package top.infopub.mgr.mkt.web;

import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.StandardEnum;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.BaseLineService;
import top.infopub.mgr.mkt.service.StandardSubService;
import top.infopub.mgr.mkt.vo.BaseLineVo;
import top.infopub.mgr.sys.service.LocationAreaDataService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.mkt.BaseLine;
import top.infopub.model.mkt.StandardSub;
import top.infopub.model.sys.Province;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 底线标准管理
 */
@Controller
@RequestMapping("/baseLine")
public class BaseLineController extends GenericFlowController {

	@Autowired
	private BaseLineService baseLineService;

	@Autowired
	private StandardSubService standardSubService;

	@Autowired
	private LocationAreaDataService locationAreaDataService;

	/**
	 * 底线标准列表
	 *
	 * @return
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request) {
		Set<Province> provinces = locationAreaDataService.selectProvincesAndsort();
		request.setAttribute("provinces", provinces);
		request.setAttribute("standardNames", StandardEnum.toJsonArray());
		return "mkt/lay-baseLine-list";
	}

	/**
	 * 底线标准列表数据
	 *
	 * @return
	 */
	@RequestMapping("/queryDataByCondition")
	@ResponseBody
	public RspVo<BaseLineVo> queryDataByCondition(BaseLineVo baseLineVo, ParamPage paramPage) {
		Page<BaseLineVo> page = new Page<BaseLineVo>();
		page.setPageNum(paramPage.getPage());
		page.setPageSize(paramPage.getLimit());
		page = baseLineService.selectPageByDomainWhere(page, baseLineVo);
		return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
	}

	/**
	 * 底线标准-新增
	 *
	 * @return
	 */
	@RequestMapping("/add")
	public String add(HttpServletRequest request) {
		return "mkt/lay-baseLine-add";
	}

	@RequestMapping("/toDetail")
	public String queryDetailById(HttpServletRequest request, String id) {
		Map<String, Object> O = new HashMap<String, Object>();
		BaseLine baseLine = baseLineService.selectById(id);
		request.setAttribute("baseLine", baseLine);
		List<StandardSub> standardSubs = standardSubService.selectByLineId(baseLine.getId());
		request.setAttribute("standardSubs", standardSubs);
		request.setAttribute("edit", "edit");
		return "mkt/lay-baseLine-add";
	}

	/**
	 * 新增底线标准
	 *
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping("/save")
	public @ResponseBody RspVo save(HttpServletRequest request, BaseLine baseLine) {
//		String baselineId = IdFactory.getUUIDUpper();
		AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();

//		baseLine.setId(baselineId);
		baseLine.setAddBy(vo.getId());
		baseLine.setAddTime(DateUtils.getCurrentDateTime());
		baseLineService.addBaseLine(baseLine);

		String[] standardName = request.getParameterValues("standardName");// 标准名称CODE
		String[] standardRule = request.getParameterValues("standardRule");// 标准规则
		String[] standardValue = request.getParameterValues("standardValue");// 标准值
		if (standardName != null && standardName.length > 0) {
			StandardSub standardSub;
			List<StandardSub> standardSubs = new ArrayList<StandardSub>();
			for (int i = 0; i < standardName.length; i++) {
				standardSub = new StandardSub();
				standardSub.setId(IdFactory.getUUIDUpper());
				standardSub.setBaseLineId(baseLine.getId());
				standardSub.setStandardCode(standardName[i]);
				standardSub.setStandardName(StandardEnum.getValue(standardName[i]));
				if (standardValue[i].equals("已入库") || standardValue[i].equals("未入库")) {
					standardSub.setStandardRule(" ");
				} else {
					standardSub.setStandardRule(standardRule[i]);
				}
				standardSub.setStandardValue(standardValue[i]);
				standardSub.setAddBy(vo.getId());
				standardSub.setAddTime(DateUtils.getCurrentDateTime());
				standardSubs.add(standardSub);
			}
			if (standardSubs.size() > 0) {
				standardSubService.insertByBatch(standardSubs);
			}
		} else {
			return ErrorCodeEnum.ERROR_COMMON_CHECK.getRspVo("标准名称未选择");
		}

		LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "底线标准 -新增地区底线标准：名称=" + baseLine.getSgCityName(), null);
		return RspVo.getSuccessResponse();
	}

	/**
	 * 编辑底线标准
	 *
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping("/edit")
	public @ResponseBody RspVo edit(HttpServletRequest request, BaseLine baseLine) {
		return  baseLineService.edit(request,baseLine);
	}

	/**
	 * 删除底线标准
	 *
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	public @ResponseBody RspVo delete(HttpServletRequest request, String id) {
		BaseLine baseLine = baseLineService.selectByPrimaryKey(id);

		standardSubService.deleteByBaseLineId(baseLine.getId());
		baseLineService.deleteByPrimaryKey(id);
		LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "底线标准 -删除地区底线标准：名称=" + baseLine.getSgCityName(), id);
		return RspVo.getSuccessResponse();
	}

	/**
	 * 查询数据字典的标准值列表
	 *
	 * @return
	 */
	@RequestMapping("/queryStandardList")
	public @ResponseBody RspVo queryStandardList() {
		return RspVo.getSuccessResponse();
	}

	/**
	 * 删除底线标准
	 *
	 * @param request
	 * @param id
	 * @return
	 */

	@RequestMapping("/updateByFlagKey")
	public @ResponseBody RspVo<?> updateByFlagKey(HttpServletRequest request, String id, String flag) {
		BaseLine baseLine = baseLineService.selectByPrimaryKey(id);
		baseLineService.updateByFlagKey(flag, id);
		LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "底线标准 -地区底线标准启用/禁用：名称=" + baseLine.getSgCityName(), id);
		return RspVo.getSuccessResponse();
	}

	@RequestMapping("/checkSgName")
	@ResponseBody
	public boolean checkSgName(HttpServletRequest request, String id, String sgCityName) {
		int checkNum = baseLineService.checkSgCityName(sgCityName, id);
		if(checkNum < 1) {
			return true;
		}
		if(StringUtils.isNotEmpty(id)) {
			BaseLine baseLine = baseLineService.selectByPrimaryKey(id);
			if(baseLine.getSgCityName().equals(sgCityName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 是否满足底线
	 * **/
	@RequestMapping("/checkIsSatisfy")
	@ResponseBody
	public List<BaseLineVo> checkIsSatisfy(String proCode){
		return baseLineService.checkIsSatisfy(proCode);
	}


	/*****
	 * 根据 省市区 查询 对应的底线标准
	 * **/
	@RequestMapping("/getStandard")
	@ResponseBody
	public List<StandardSub>  getStandard (String address){
			return  baseLineService.getStandard(address);
	}
}
